package com.codeest.geeknews.presenter.main;

import com.alibaba.fastjson.JSON;
import com.codeest.geeknews.base.RxPresenter;
import com.codeest.geeknews.base.contract.main.LastfmContract;
import com.codeest.geeknews.model.DataManager;
import com.codeest.geeknews.model.bean.LastfmAlbumBean;
import com.codeest.geeknews.util.RxUtil;
import com.codeest.geeknews.widget.CommonSubscriber;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import javax.inject.Inject;

import io.reactivex.functions.Function;

/**
 * Created by admin on 2017/8/22.
 */

public class LastfmPresenter extends RxPresenter<LastfmContract.View> implements LastfmContract.Presenter {

    private DataManager mDataManager;

    @Inject
    public LastfmPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }


    @Override
    public void getAlbumInfo(String artist, String album) {
        addSubscribe(mDataManager.fetchAlbunInfo(artist, album)
                .compose(RxUtil.<String>rxSchedulerHelper())
                .map(new Function<String, String>() {
                    @Override
                    public String apply(String dailyListBean) {

                        return dailyListBean;
                    }
                })
                .subscribeWith(new CommonSubscriber<String>(mView) {
                    @Override
                    public void onNext(String dailyListBean) {
                        Gson gson = new GsonBuilder().create();
                        LastfmAlbumBean lastfmAlbumBean = gson.fromJson(dailyListBean, LastfmAlbumBean.class);
                        mView.showContent(lastfmAlbumBean);
                    }
                })
        );
    }

    public abstract static class ViewEx implements LastfmContract.View {
        @Override
        public void showErrorMsg(String msg) {
        }

        @Override
        public void useNightMode(boolean isNight) {
        }

        @Override

        public void stateError() {
        }

        @Override

        public void stateEmpty() {
        }

        @Override

        public void stateLoading() {
        }

        @Override
        public void stateMain() {
        }

    }

    public void getAlbumInfo(String artist, String album, final ViewEx view) {
        addSubscribe(mDataManager.fetchAlbunInfo(artist, album)
                .compose(RxUtil.<String>rxSchedulerHelper())
                .map(new Function<String, String>() {
                    @Override
                    public String apply(String lastfm) {
                        return lastfm.replaceAll("#text", "text").replaceAll("@attr", "attr");
                    }
                })
                .subscribeWith(new CommonSubscriber<String>(view) {
                    @Override
                    public void onNext(String lastfm) {
                        LastfmAlbumBean lastfmAlbumBean = JSON.parseObject(lastfm, LastfmAlbumBean.class);
                        view.showContent(lastfmAlbumBean);
                    }
                })
        );
    }
}
